PyTorch Artificial Neural Network (ANN)

Dataset

a random n-class classification dataset can be generated using sklearn.datasets.make_classification. Here, we generate a dataset with two features and 6000 instances. Moreover, the dataset is generated for multiclass classification with five classes.

Modeling

Train and Test sets

One of the efficient methods of splitting a dataset into random train and test subsets is using sklearn.model_selection.train_test_split.

Artificial Neural Networks

Artificial neural networks (ANN), also know as connectionist systems, are computing system that is inspired by animal brain neural networks. ANNs are composed of artificial neurons. These neurons are connected using connections. For a given neuron, there are multiple input and output connections.

Artificial neural network components:

There is also an element of (machine) learning. Roughly speaking, learning consists of two parts forward propagation and Backpropagation. Together with a cost function, the algorithm runs iteratively with the object of decreasing Loss and increasing the accuracy.

In this article, we develop a Pytorch Artificial Neural Network model. The number of layers can be adjusted, of course. There are a large number of resources on choosing the right number of hidden layers such as [1]. Generally speaking, we do not recommend adding too many hidden layers. For a large number of problems, one hidden layer is sufficient.

PyTorch Artificial Neural Network with a Hidden Layer

A single hidden layer ANN in PyTorch can be defined as follows.

Let's define some function by which we can analyze the performance of the modeling.

Model Optimization Plot

Confusion Matrix

The confusion matrix allows for visualization of the performance of an algorithm.

Plot Classification

PyTorch Artificial Neural Network with Multiple Layers.

A three-layer ANN in PyTorch can be defined as follows.

Model Optimization Plot

Confusion Matrix

The confusion matrix allows for visualization of the performance of an algorithm.

Plot Classification


Refrences

  1. Stathakis, D. (2009). How many hidden layers and nodes?. International Journal of Remote Sensing, 30(8), 2133-2147.
  2. Artificial neural network. Retrieved June 02, 2020, from https://en.wikipedia.org/wiki/Artificial_neural_network.